<?php

namespace app\admin\controller\basic;

use app\common\controller\Backend;
use think\Db;
use fast\Random;

/**
 * 年段列表
 */
class Gradelevel extends Backend
{

    /**
     * @var \app\common\model\Gradelevel
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\common\model\Gradelevel;
        $ruleList = Db::name("study_level")->select();
        // 如果查询结果为空，赋值为空数组
        $this->rulelist = is_array($ruleList) ? $ruleList : [];
        $ruledata = [0 => __('None')];
        foreach ($this->rulelist as $k => &$v) {
            $ruledata[$v['id']] = $v['level_name'];
        }
        $this->view->assign('ruledata', $ruledata);
    }

    /**
     * 查看
     */
    public function index(){
        if ($this->request->isAjax()) {

            // 获取请求参数
            $params = $this->request->param();

            // 查询数据（带分页）
            $list = $this->model
                ->with('studylevel')
                ->order($params['sort'] ?? 'id', $params['order'] ?? 'desc')
                ->paginate([
                    'list_rows' => $params['limit'] ?? 10,
                    'page'      => ($params['offset'] / $params['limit'] + 1) ?? 1
                ]);
            // 返回标准JSON格式
            $result = [
                "total" => $list->total(),
                "rows"  => $list->items()
            ];
            return json($result);
        }
        return $this->view->fetch();
    }

}